package com.topvision.myviewtest.gscview;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.topvision.myviewtest.R;
import com.topvision.myviewtest.gscview.adapter.L_ExtendAdapter;
import com.topvision.myviewtest.gscview.ball.CoordinateSystemView;
import com.topvision.myviewtest.gscview.ball.RunView;
import com.topvision.myviewtest.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author lucy
 * @date 2018-10-17 13:26
 * @description //TODO
 */

public class GcsActivity extends AppCompatActivity {
    private static final String TAG = "GcsActivity";
    @BindView(R.id.rootview)
    FrameLayout mRootView;
    private Unbinder mBind;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        setContentView(R.layout.activity_gcs);
        mBind = ButterKnife.bind(this);
//        showPieView();
//        showSearchPathView();
//        showButtonView();
//        showTouchDispatch();
//        showGradientView();
//        startActivity(new Intent(this,WapperActivity.class));
//        new LiveWallpaperService().set2WallPaper(this);
//        showCoordinateSystemView();
        showBallView();
    }

    private void showButtonView() {
        ButtonView view = new ButtonView(this);
        mRootView.addView(view);
    }


    private void showPieView() {
        PieView view = new PieView(this);
        mRootView.addView(view);
        List<PieData> data = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            data.add(new PieData(String.format("Name%s", i), 100 * (i + 1)));
        }
        view.setData(data);
    }

    private void showSearchPathView() {
        SearchPathView view = new SearchPathView(this);
        mRootView.addView(view);

    }

    private void showTouchDispatch() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_extend, null);
        final ExtendView extendView = (ExtendView) view.findViewById(R.id.extendview);
        extendView.setOnExtendStateChangeListener(new ExtendView.OnExtendStateChangeListener() {
            @Override
            public void onExtend() {
                Log.d(TAG, "onExtend");
            }

            @Override
            public void onClose() {
                Log.d(TAG, "onClose");
            }
        });
        // 包含Recyclerview
//        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        List<String> list = new ArrayList<>();
//        for (int i = 0; i < 100; i++) {
//            list.add(String.valueOf(i));
//        }
//        R_ExtendAdapter adapter = new R_ExtendAdapter(R.layout.item_recycler_study, list);
//        recyclerView.setAdapter(adapter);
        // 包含listview
        ListView listView = (ListView) view.findViewById(R.id.listview);
        listView.setAdapter(new L_ExtendAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ToastUtils.showToast(GcsActivity.this, String.valueOf(position));
                if (position % 2 == 0) {
                    extendView.extend();
                } else {
                    extendView.close();
                }
            }
        });

        mRootView.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    private void showGradientView() {
        StatusBarUtil.setStatusBarTransparent(this);
        GradientView view = new GradientView(this);
        mRootView.addView(view);
    }

    private void showCoordinateSystemView() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        CoordinateSystemView view = new CoordinateSystemView(this);
        mRootView.addView(view);
    }

    private void showBallView() {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        RunView view = new RunView(this);
        mRootView.addView(view);
    }

    @Override
    protected void onDestroy() {
        if (mBind != null) {
            mBind.unbind();
        }
        super.onDestroy();
    }
}
